package com.ljy.my_study.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/** 
* @author James
* @date 2018年6月1日 
*/
public class SocketServer {
	public static void main(String[] args) {
		ServerSocket server=null;
		try {
			server=new ServerSocket(8888);
				Socket socket=server.accept();
				PrintWriter output=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
				BufferedReader input=new BufferedReader(new InputStreamReader(socket.getInputStream()));
				while(true) {
					String line=input.readLine();
					if(line==null) {
						break;
					}
					System.out.println("服务端收到的信息："+line);
					output.println("hello 客户端！");
					output.flush();
				}
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(server!=null) {
				try {
					server.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
